package com.dk.controller;

import com.dk.pojo.TbAccident;
import com.dk.result.Result;
import com.dk.service.TbAccidentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@Api
@ApiOperation("事故登记接口")
@RestController
@RequestMapping("accident")
public class TbAccidentController {

    @Autowired
    private TbAccidentService accidentService;



    @GetMapping("selectAll")
    @ApiOperation(value = "查询全部")
    public Result selectAll(){
        try {
            List<TbAccident> accidents = accidentService.selectAll();
            return Result.OK().setData(accidents);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }



    @GetMapping("getAccidentById/{instrumentId}")
    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id", value = "文书编号id", required = true, paramType = "path")
    public Result getAccidentsById(@PathVariable String instrumentId){
        try {
            TbAccident accident = accidentService.getAccidentsById(instrumentId);
            return Result.OK().setData(accident);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }

    }



    @PostMapping("addAccidents")
    @ApiOperation(value = "新增数据")
    @ApiImplicitParam(name = "accident", value = "事故登记实体", required = true, paramType = "body")
    public Result addAccidents(@RequestBody TbAccident accident){
        try {
            accidentService.addAccidents(accident);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @PutMapping("updateAccidentById")
    @ApiOperation(value = "根据id修改")
    @ApiImplicitParam(name = "accident", value = "事故登记实体", required = true, paramType = "body")
    public Result updateAccidentById(@RequestBody TbAccident accident){
        try {
            accidentService.updateAccidentById(accident);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    /**
     * 根据id删除
     * @param instrumentId
     * @return
     */
    @DeleteMapping("deleteAccidentById/{instrumentId}")
    @ApiOperation(value = "根据id删除")
    @ApiImplicitParam(name = "instrumentId", value = "文书编号id", required = true, paramType = "path")
    public Result deleteAccidentById(@PathVariable String instrumentId){
        try {
            accidentService.deleteAccidentById(instrumentId);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }

    }
}
